﻿using ConfExpert.Forms.Base;
using DevTeam;

using System;
using System.Data;

namespace ConfExpert.Forms.Depo
{
    public partial class ArtikelMetrajEsleme : XForm
    {
        public DataSet d;
        public ArtikelMetrajEsleme(DataSet d)
        {
            InitializeComponent();
            this.d = d;
        }

        private void ArtikelMetrajEsleme_Load(object sender, EventArgs e)
        {
            gridView1.Init(d.Tables[0]);
            gridView2.Init(d.Tables[1]);
            gridView2.FormatAsNumber(1, "TotalQty");
            gridView2.AddSummary("TotalQty");
            gridView1.FormatAsNumber(1, "YuklenenMt");
            gridView1.AddSummary("YuklenenMt");

            gridView1.MakeReadOnly();
            gridView2.MakeReadOnly("ProductId");

            gridView1.BestFitColumns();
            gridView2.BestFitColumns();

            gridView1.SetViewCaption("Olması Gereken");
            gridView2.SetViewCaption("Dosyada Geçen");

            bbiKaydet.ItemClick += bbiKaydet_ItemClick;
        }

        void bbiKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //cndt: boş olmasın, artikel ve metrajları tutsun!
            bool ok = true;

            gridView2.EndEditing();
            DataTable t1 = gridView1.DataTable();
            DataTable t2 = gridView2.DataTable();
            foreach (DataRow r in t1.Rows)
            {
                DataRow[] rx = t2.Select("ProductId=" + r["ProductId"]);
                if (rx.Length < 1 ||
                    Math.Abs(rx[0]["TotalQty"].ToDecimal(-1) - r["YuklenenMt"].ToDecimal(-1)) > 1)
                {
                    ok = false;
                    break;
                }
            }

            if (ok)
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
